Learn button debounce principle and adaptive programming, Zynq_7030 button hardware circuit – zynq xc7z030 board – FII-PE7030 Experiment 4 – Button Debounce
Experiment 4 Button Debounce
4.1 Experiment Objective
- Review the design process of the shifting LED
- Learn button debounce principle and adaptive programming
- Learn the connection and use of the Zynq_7030 button hardware circuit
- Comprehensive application button debounce and other conforming programming
4.2 Experiment Implement
- Control the movement of the lit LED by pressing the button
- Each time the button is pressed, the lit LED moves one bit.
- When the left shift button is pressed, the LED moves to the left, presses the right button, and the LED moves to the right.
4.3 Experiment
4.3.1 Introduction to Button and Debounce Principle
- Introduction to button
The on-board button is a common push button, which is valid when pressed, and automatically pops up when released. A total of eight, respectively, PB1 (MENU), PB2 (UP), PB3 (RETURN), PB4 (LETF), PB5 (OK), PB6 (RIGHT), PB7 (DOWN) and a hardware reset button (RESET). As shown in Figure 4.1.
Figure 4.1 Button physical picture
Introduction to button debounce
As long as mechanical buttons are used, instability should be considered. Usually, the switches used for the buttons are mechanical elastic switches. When the mechanical contacts are opened and closed, due to the elastic action of the mechanical contacts, a push button switch does not immediately turn on when closed, nor is it off when disconnected. Instead, there is some bouncing when connecting and disconnecting. See Figure 4.2.
The length of the button’s stable closing time is determined by the operator. It usually takes more than 100ms. If you press it quickly, it will reach 40-50ms. It is difficult to make it even shorter. The bouncing time is determined by the mechanical characteristics of the button. It is usually between a few milliseconds and tens of milliseconds. To ensure that the program responds to the button’s every on and off, it must be debounced. When the change of the button state is detected, it should not be immediately responding to the action, but waiting for the closure or the disconnection to be stabilized before processing. Button debounce can be divided into hardware debounce and software debounce.
Figure 4. 2 Button bounce principle
In most of cases, we use software or programs to achieve debounce. The simplest debounce principle is to wait for a delay time of about 10ms after detecting the change of the button state, and then perform the button state detection again after the bounce disappears. If the state is the same as the previous state just detected, the button can be confirmed. The action has been stabilized. This type of detection is widely used in traditional software design. However, as the number of button usage increases, or the buttons of different qualities will react differently. If the delay is too short, the bounce cannot be filtered out. When the delay is too long, it affects the sensitivity of the button.
4.3.2 Hardware Design
The schematics (part) of the button is shown in Figure 4.3. You can see that one side of the button (P1, P2) is connected to VCC, and the other side (P3, P4) is connected to the FPGA. At the same time, it is grounded through a 5.11K resistor. In the normal state, the button is left floating. At this time, the potential at button P3 is 0, so the input value to the FPGA is 0. When the button is pressed, both sides of the button are turned on. At this time, the potential at button P3 is VCC3.3V, so the button inputs a value of 1 into the FPGA. So the on-board push button is high effective.
Figure 4.3 Schematics of the push buttons
4.3.3 Program Design
Refer to the previous experiments for the frequency division module and new push buttons debounce module is introduced below.
Figure 4.4 Button deboucne flow chart
This chapter introduces an adaptive button debounce method: starts timing when a change in the state of the button is detected. If the state changes within 10ms, the button bouncing exists. It returns to the initial state, clears the delay counter, and re-detects the button state until the delay counter counts to 10ms. The same debounce method is used for pressing and releasing the button. The flow chart is shown in Figure 4. 4.Case 0 and 1 debounce the button press state. Case 2 and 3 debounce the button release state. After finishing the whole debounce procedure, the program outputs a synchronized clock pulse.
module pb_ve (
input sys_clk, input sys_rst, input ms_f, input keyin, output keyout ); reg keyin_r; reg keyout_r; reg [1:0] ve_key_st; reg [3:0] ve_key_count; always @ (posedge sys_clk) begin keyin_r <= keyin; end always @ (posedge sys_clk) begin if (sys_rst) begin keyout_r <= 1’b0; ve_key_count <= 0; ve_key_st <= 0; end else case (ve_key_st) 0 : begin keyout_r <= 1’b0; ve_key_count <= 0; if (keyin_r) ve_key_st <= 1; end 1 : begin if (!keyin_r) ve_key_st <= 0; else begin if (ve_key_count == 10) ve_key_st <= 2; else if (ms_f) ve_key_count <= ve_key_count + 1’b1; end end 2 : begin ve_key_count <= 0; if (!keyin_r) ve_key_st <= 3; end 3 : begin if (keyin_r) ve_key_st <= 2; else begin if (ve_key_count == 10) begin ve_key_st <= 0; keyout_r <= 1’b1; end else if (ms_f) ve_key_count <= ve_key_count + 1’b1; end end default : ; endcase end assign keyout = keyout_r; endmodule |
Button-controlled LED display module
module led_shift(
input sys_clk, input rst, input key_left, input key_right, input s_f, output reg [7:0] led ); always @ (posedge sys_clk) begin if (rst) led <= 8’hff; else begin if (key_left) begin if (led == 8’hff) led <= 8’b0000_0001; else led <= {led[6:0], led[7]}; end else if (key_right) begin if (led == 8’hff) led <= 8’b1000_0000; else led <= {led[0], led[7:1]}; end end end endmodule |
In the reset state, all the 8-bit LED is on. When the button is pressed, only one LED is on. After that, each time the push button is pressed, the LED moves according to the corresponding signal.
4.4 Experiment Verification
The first step: add constraints and assign the pins
The pin assignment is shown in Table 4.1.
Table 4.1 Button debounce experiment pin mapping table
Signal Name | Network Name | FPGA Pin | Port Description |
inclk_p | SYSCLK_P | AC13 | Input clock (Differential)
200MHz |
inclk_n | SYSCLK_N | AD13 | |
rst | GPIO_SW_2 | F4 | External reset |
left | GPIO_SW_3 | D4 | Left shift signal |
right | GPIO_SW_5 | F2 | Right shift signal |
led[0] | GPIO_DIP_SW0 | A17 | 8-bit LED |
led[1] | GPIO_DIP_SW1 | E8 | |
led[2] | GPIO_DIP_SW2 | C6 | |
led[3] | GPIO_DIP_SW3 | B9 | |
led[4] | GPIO_DIP_SW4 | B6 | |
led[5] | GPIO_DIP_SW5 | H6 | |
led[6] | GPIO_DIP_SW6 | H7 | |
led[7] | GPIO_DIP_SW7 | G9 |
The second step: run the implementation, generate bitstream files, and verify the board
After successfully downloading the generated programmable bitstream file to the Zynq_7030 development board, the experimental phenomena are shown in Figures 4.5 to 4.7.
Figure 4.5 Experiment Result(reset)
When the right shift button is pressed, the highest LED lights up. See Figure 4.6.
Figure 4.6 Experiment result(one right shift)
Press the right shift button again and the LED will move one bit to the right. See Figure 4.7.
Figure 4.7 Experiment result(another right shift)